<template>
  <!--查看答辩组学生（校管理员）-->
  <div class="assign-reply-stu">
    <InnerBreadcrumb v-model="indexKey" :path="breadPath" :showBack="true" />
    <div class="edu-page-warp" v-show="indexKey === 'MAIN'">
      <!--查看老师/学生详情弹框-->
      <TeacherStuInfoDialog :viewDetailData="viewDetailData" :dialogVisible="dialogVisible" :viewType="viewType" @taacher-stu-close="closeDialog"></TeacherStuInfoDialog>

      <condition-card @reset="reset" @search="searchList" label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.graduationYear')">
              <el-date-picker class="search-item" :clearable="false" v-model="queryForm.graduationYear" @change="handleGradeChange" value-format="yyyy" type="year" :placeholder="$t('graduationDesign.pleaseChoose')">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.college')">
              <el-select v-model="queryForm.collegeId" filterable @change="handleCollegeChange" class="search-item" clearable>
                <el-option v-for="item in source.collegeOptions" :key="item.collegeId" :label="item.collegeIdI18n" :value="item.collegeId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.major')">
              <el-select v-model="queryForm.subjectId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectOptions" :key="item.subjectId" :label="item.subjectName" :value="item.subjectId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.replyTime')">
              <el-date-picker v-model="queryForm.defenseStartTime" class="search-item" type="date" clearable value-format="yyyy-MM-dd HH:mm:ss" :placeholder="$t('graduationDesign.pleaseChoose')">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.replyGroup')">
              <el-input v-model="queryForm.defenseGroupName" :placeholder="$t('graduationDesign.pleaseEnter')" class="search-item" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card>
        <div class="edu-page-title">{{$t('graduationDesign.viewReplyGroupStudent')}}</div>
        <div class="edu-opertion-bar">
          <div class="float-left">
            <el-input v-model="queryForm.defenseGroupLeaderName" :placeholder="$t('graduationDesign.pleaseEnterReplyLeaderName')" style="width:300px" @keyup.enter.native="searchList" clearable>
              <i slot="suffix" class="el-icon-search" style="margin-top:10px;cursor:pointer" @click="searchList"></i>
            </el-input>
          </div>
          <div class="float-right">
            <el-button @click="onExport" v-loading.fullscreen.lock="loading" element-loading-text="Loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, .48)">{{$t('graduationDesign.export')}}</el-button>
          </div>
        </div>

        <edu-table ref="assignReplyStuTable" :url="getList" @selection-change="handleSelectChange">
          <edu-table-column :label="$t('graduationDesign.graduationYear')" prop="graduationYear" min-width="80"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.college')" prop="collegeIdI18n" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.major')" prop="subjectIdI18n" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyGroup')" prop="defenseGroupName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyLocation')" prop="defenseLocation" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.startTime')" prop="defenseStartTime" min-width="110"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyGroupLeader')" prop="defenseGroupLeaderName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyGroupTeacher')" min-width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="onDetail(scope.row, 'TEACHER')">{{scope.row.defenseGroupTeacherNum}}</el-button>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyGroupStudent')" min-width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="onDetail(scope.row, 'STU')">{{scope.row.defenseGroupStudentNum}}</el-button>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.operation')" min-width="120">
            <template slot-scope="scope">
              <el-button type="text" @click="onView(scope.row)">{{$t('graduationDesign.view')}}</el-button>
            </template>
          </edu-table-column>
        </edu-table>
      </el-card>
    </div>
    <!--查看-->
    <div v-if="indexKey === 'VIEW'">
      <ReplyTeam ref="replyTeam" :actionType="actionType" :source="insource" :replyTeamForm="replyTeamForm"></ReplyTeam>
    </div>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/assignReplyStudent";
import tools from 'common/src/views/underGraduationDesign/tools.js';
export default {
  name: 'viewReplyStudent',
  data() {
    return {
      api: API,
      getList: '/api/designservice/defenseGroup/list',
      queryForm: {
        graduationYear: '',
        collegeId: '',
        subjectId: '',
        defenseStartTime: '',
        defenseGroupName: '',
        defenseGroupLeaderName: ''
      },
      indexKey: 'MAIN',
      source: {
        collegeOptions: [], // 学院
        subjectOptions: [] // 专业
      },
      assignSelectedData: [], // 勾选的列表数据
      breadPath: {
        label: this.$t('graduationDesign.viewReplyGroupStudentSchoolAdmin'),
        key: "MAIN",
        children: [
          {
            key: "VIEW",
            label: this.$t('graduationDesign.view')
          }
        ]
      },
      loading: false,
      dialogVisible: false,
      viewDetailData: [], // 查看教师学生列表
      viewType: '',
      replyTeamForm: {},
      actionType: '', // 查看
      insource: {
        committeeOptions: [], // 答辩委员会
        leaderOptions: [], // 答辩组长
        teacherOptions: [], // 老师
        studentOptions: [], // 学生
        foreignStudentOptions: [] // 外专业学生
      }
    };
  },
  mounted() {
    this.queryForm.graduationYear = new Date().getFullYear() + '';
    this.getCollege();
    this.searchList();
  },
  methods: {
    searchList() {
      this.$refs.assignReplyStuTable.search(this.queryForm);
    },
    reset() {
      this.queryForm = {
        graduationYear: '',
        collegeId: '',
        subjectId: '',
        defenseStartTime: '',
        defenseGroupName: '',
        defenseGroupLeaderName: ''
      };
      this.source.subjectOptions = [];
      this.queryForm.graduationYear = new Date().getFullYear() + '';
      this.searchList();
    },
    handleSelectChange(val) {
      this.assignSelectedData = val;
    },
    // 查学院
    getCollege() {
      this.api.getCollege().then(res => {
        this.source.collegeOptions = res;
      });
    },
    handleGradeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.collegeId) {
        this.handleCollegeChange(this.queryForm.collegeId);
      }
    },
    // 根据毕业年份和学院查专业
    handleCollegeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.graduationYear) {
        const params = {
          grade: this.queryForm.graduationYear,
          college: val
        };
        this.api.getSubject(params).then(res => {
          this.source.subjectOptions = res.data;
        }).catch();
      }
    },
    // 查看答辩组老师 学生
    onDetail(row, type) {
      this.viewType = type;
      let roleType = type === 'TEACHER' ? '0' : '1';
      this.api.getTeacherStuInfo(row.id, roleType).then(res => {
        this.viewDetailData = res.data;
        this.dialogVisible = true;
      }).catch();
    },
    // 关闭
    closeDialog() {
      this.dialogVisible = false;
    },
    // 下拉框格式化
    formatOptions(data = [], label, value) {
      let dataset = [];
      data.forEach(item => {
        dataset.push({
          label: item[label],
          value: item[value],
        });
      });
      return dataset;
    },
    // 拼接名称
    getNameStr(data = [], name = "") {
      let dataset = [];
      data.forEach(item => {
        dataset.push(item[name]);
      });
      return dataset.join(",");
    },
    // 查答辩组信息
    onView(row) {
      this.api.getDetail(row.id).then(res => {
        this.actionType = "VIEW";
        this.indexKey = "VIEW";
        this.replyTeamForm = { ...res.data };
        this.replyTeamForm.graduationYear = row.graduationYear ? row.graduationYear + '' : '';
        this.replyTeamForm.committeeMemberNames = this.getNameStr(res.data.defenseCommitteeMemberList, "teacherName");
        this.replyTeamForm.teacherNames = this.getNameStr(res.data.defenseGroupTeacherList, "teacherName");
        this.replyTeamForm.studentNames = this.getNameStr(res.data.defenseGroupStudentList, "studentName");
        this.replyTeamForm.foreignStudentNames = this.getNameStr(res.data.defenseGroupOtherSubjectStudentList || [], "studentName");
        this.insource = {
          committeeOptions: this.formatOptions(res.data.defenseCommitteeMemberList, "teacherName", "teacherId"), // 答辩委员会
          leaderOptions: [{ label: res.data.defenseGroupLeaderName, value: res.data.defenseGroupLeader }], // 答辩组长
          teacherOptions: this.formatOptions(res.data.defenseGroupTeacherList, "teacherName", "teacherId"), // 老师
          studentOptions: this.formatOptions(res.data.defenseGroupStudentList, "studentName", "studentId"), // 学生
          foreignStudentOptions: this.formatOptions(res.data.defenseGroupOtherSubjectStudentList || [], "studentName", "studentId") // 外专业学生
        };
      }).catch();
    },
    // 导出
    onExport() {
      const params = {
        condition: this.queryForm,
        selectList: this.assignSelectedData
      };
      this.api.export(params).then(res => {
        if (res.code == 200) {
          // this.delayRequest(res.data.key);
          tools.downloadFile(res.data.key, "查看答辩组学生(校管理员).xls", null, this);
        }
      });
    },
    // 查文件导出状态，等待导出
    async delayRequest(key) {
      this.loading = true;
      await new Promise(resolve => setTimeout(resolve, 3000));
      this.api.getExportStatus(key).then(res => {
        this.loading = false;
        if (res.code === 200 && res.data.status) {
          let path = 'api/designservice/common/export?path=' + res.data.path;
          window.open(path);
        } else {
          this.delayRequest(key);
        }
      }).catch(err => {
        this.loading = false;
        this.delayRequest(key);
      });
    }
  },
  components: {
    ReplyTeam: () => import('../assignReplyStudent/components/replyTeam'),
    TeacherStuInfoDialog: () => import('../assignReplyStudent/components/teacherStuInfoDialog')
  }
}
</script>
<style lang="scss" scoped>
.assign-reply-stu {
    .search-item {
        width: 100%;
    }
}
</style>
